const fp = require('lodash/fp');
const { Maybe, Container } = require('./support');

// 练习1
const maybe = Maybe.of([5, 6, 1]);
const ex1 = (x) => fp.map(fp.add(x));
console.log(maybe.map(ex1(1))._value); // [ 6, 7, 2 ]
console.log(maybe.map(ex1(2))._value); // [ 7, 8, 3 ]


// 练习2
const xs = Container.of(['do', 'me', 'fa', 'so', 'la', 'ti', 'do']);
const ex2 = xs => xs.map(fp.first)._value;
console.log(ex2(xs)); // do

// 练习3
const safeProp = fp.curry(function(x, o) {
  return Maybe.of(o[x]);
});
const user = {
  id: 2,
  name: 'Albert',
};
const ex3 = user => fp.first(safeProp('name')(user)._value);
console.log(ex3(user)); // A

// 练习4
// let ex4 = function(n) {
//   if(n) {
//     return parseInt(n);
//   }
// }
let ex4 = n => Maybe.of(n).map(parseInt)._value;
console.log(ex4(null)); // null
console.log(ex4()); // undefined
console.log(ex4('996')); // 996